﻿/*----------------------------------------------------------------------------------------
*
*  Copyright 2019, Gao Hai Hui, <fromzeropoint@126.com>.  All rights reserved.
*  https://gitee.com/helloworldghh/xoskit.git
*  Use of this source code is governed by a MIT license
*  that can be found in the License file.
*
----------------------------------------------------------------------------------------*/
#include "../import/head.h"
#include "../global/head.h"
#include "id_card.h"

namespace xos_common
{

	/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
	// 

	id_card::id_card()
	{
	}

	id_card::~id_card()
	{
	}

	/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
	// 

	int id_card::is_leapyear( int year )
	{
		if( ( ( year % 4 == 0 ) && ( year % 400 != 0 ) ) || ( year % 400 == 0 ) )
			return 1;
		return 0;
	}

	int id_card::getyear( const char* input )
	{
		int year = 0;
		year = ( input[6] - '0' ) * 1000 + ( input[7] - '0' ) * 100 +
			( input[8] - '0' ) * 10 + ( input[9] - '0' );
		return year;
	}

	int id_card::getmonth( const char* input )
	{
		int month = 0;
		month = ( input[10] - '0' ) * 10 + ( input[11] - '0' );
		return month;
	}

	int id_card::getday( const char* input )
	{
		int day = 0;
		day = ( input[12] - '0' ) * 10 + ( input[13] - '0' );
		return day;
	}

	int id_card::check_1to17( const char* input )
	{
		int i;
		for( i = 0; i<17; i++ )
			if( !( input[i] >= '0' && input[i] <= '9' ) )
				return 0;
		return 1;
	}

	int id_card::check18( const char* input )
	{
		char ch = input[17];
		if( ( ch == 'x' ) || ( ch >= '0' && ch <= '9' ) )
			return 1;
		else
			return 0;
	}

	int id_card::chech_year_month_day( int year, int month, int day )
	{
		int leap;
		leap = is_leapyear( year );
		if( year<1900 || year>2100 )
			return 4;
		if( month<1 || month>12 )
			return 5;
		switch( month )
		{
		case 1:
		case 3:
		case 5:
		case 7:
		case 8:
		case 10:
		case 12:
			{
				if( day<1 || day>31 )
					return 6;
				break;
			}
		case 4:
		case 6:
		case 9:
		case 11:
			{
				if( day<1 || day>30 )
					return 6;
				break;
			}
		case 2:
			{
				if( leap )
				{
					if( day<1 || day>29 )
						return 6;
					break;
				}
				else
				{
					if( day<1 || day>28 )
						return 6;
					break;
				}
			}
		}
		return 0;
	}

	int id_card::verifyIDCard( const char* input )
	{
		int year, month, day;
		if( strlen( input ) != 18 )
			return 1;
		else
		{
			if( !check_1to17( input ) )
				return 2;
			else
			{
				if( !check18( input ) )
					return 3;
				else
				{
					year = getyear( input );
					month = getmonth( input );
					day = getday( input );
					return chech_year_month_day( year, month, day );
				}
			}
		}
	}

	bool id_card::is_valid_id_card( const char * lpszIdCard )
	{
		bool bRet = false;
		bRet = 0 == verifyIDCard( lpszIdCard );
		return bRet;
	}

} // xos_common
